package net.sevear.chaos.view.recommend
{
	import com.adobe.serialization.json.JSON;
	import com.douban.utils.Format;
	
	import flash.display.JointStyle;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import net.sevear.chaos.ChaosTrackManager;
	import net.sevear.chaos.event.ChaosTrackEvent;
	import net.sevear.chaos.event.ContactEvent;
	import net.sevear.chaos.event.RecommendEvent;
	import net.sevear.chaos.event.p2p.P2pEvent;
	import net.sevear.chaos.p2p.P2PSitesManager;
	import net.sevear.chaos.recommend.ContactManager;
	import net.sevear.chaos.recommend.RecommendManager;
	import net.sevear.chaos.utils.DataUtil;
	import net.sevear.chaos.vo.ChaosEvtInfo;
	import net.sevear.chaos.vo.TrackInfo;
	import net.sevear.chaos.vo.UserInfo;
	
	import spark.components.Group;
	import spark.components.Label;
	
	public class UserGroup extends Group
	{
		public static const USER_GROUP_SELECTED_CHANGE:String = "User Group Selected Change";
		private var MAX_COUNT:int = 6;
		private var _list:Vector.<UserQuickSlectViewItem>;
		private var _voList:Array;
		private var _clearBtn:Label;
		private var _addMoreLbl:Label;
		public function UserGroup(width:int)
		{
			super();
			this.width = width;
			_list = new Vector.<UserQuickSlectViewItem>(MAX_COUNT);
			var i:int = 0;
			for(;i<MAX_COUNT;i++){
				var item:UserQuickSlectViewItem = new UserQuickSlectViewItem(this.width);
				item.x = 20;
				item.y = i*(item.height);
				item.user = null;
				this.addElement(item);
				item.addEventListener(Event.CHANGE,onSelectedValueChange);
				_list[i] = item;
			}
			
			_clearBtn = new Label();
			_clearBtn.text = "Clear";
			_clearBtn.addEventListener(MouseEvent.CLICK,onClearLblClick);
			_clearBtn.top = 0;
			_clearBtn.x = 5;
			_clearBtn.buttonMode = true;
			_clearBtn.useHandCursor = true;
			_clearBtn.setStyle("textDecoration","underline");
			
			_addMoreLbl = new Label();
			_addMoreLbl.text = "Send To More...";
			_addMoreLbl.top = 0;
			_addMoreLbl.x = 20;
			_addMoreLbl.buttonMode = true;
			_addMoreLbl.useHandCursor = true;
			_addMoreLbl.setStyle("textDecoration","underline");
			ContactManager.getInstance().addEventListener(ContactEvent.GET_NEW_LIST,onContactListChanged);
			RecommendManager.getInstance().addEventListener(RecommendEvent.GET_CONTACTS_RECENT_LIKES,onGetContactsRecentLike);
			RecommendManager.getInstance().addEventListener(RecommendEvent.RECEIVER_LIKED,onReceiverLiked);
			P2PSitesManager.getInstance().addEventListener(P2pEvent.FRIEND_SITE_ON_AIR,onFriendSiteOnAir);
			P2PSitesManager.getInstance().addEventListener(P2pEvent.FRIEND_SITE_NOT_ON_AIR,onFriendSiteNotOnAir);
		}
		
		public function show():void{
			this.visible = true;
		}
		
		public function hide():void{
			this.visible = false;
		}
		
		
		public function clearAllSelected():void{
			for (var i:int=0;i<_voList.length;i++){
				(_voList[i] as UserInfo).selected = false;
				(_list[i] as UserQuickSlectViewItem).selected = false;
			}
		}
		
		private function set users(value:Array):void{
			if(value!=null){
				_voList = value;
				clearAllSelected();
				updateView();
			}
		}
		
		public function get selectedUIDs():Array{
			var arr:Array = [];
			for each(var user:UserInfo in _voList){
				if(user.selected) 
					arr.push(user.dbID);
			}
			return arr;
		}
		
		public function get selectedUsers():Array{
			var arr:Array = [];
			for each(var user:UserInfo in _voList){
				if(user.selected) arr.push(user);
			}
			return arr;
		}
		
		public function get colors():Array{
			var arr:Array = [];
			for (var i:int=0;i<_voList.length;i++){
				if((_voList[i] as UserInfo).selected) 
				{
					arr.push((_voList[i] as UserInfo).color);
				}
			}
			return arr;
		}
		
		private function updateView():void{
			for(var i:int = 0;i<MAX_COUNT;i++){
				var elem:UserQuickSlectViewItem = _list[i] as UserQuickSlectViewItem;
				if(_voList.length>i){
					elem.user = _voList[i] as UserInfo;
				}else{
					elem.user = null;
				}
			}
		}
		
		private function onContactListChanged(e:ContactEvent):void{
			this.users = ContactManager.getInstance().contacts.source;
			var uids:Array = [];
			for each(var userInfo:UserInfo in _voList){
				uids.push(userInfo.dbID);
			}
			RecommendManager.getInstance().getContactsRecentLike(uids);
		}
		
		private function onGetContactsRecentLike(e:RecommendEvent):void{
			var rst:Object = e.data;
			for(var i:int = 0;i<MAX_COUNT;i++){
				var elem:UserQuickSlectViewItem = _list[i] as UserQuickSlectViewItem;
				if(elem.user!=null && rst.hasOwnProperty(elem.user.dbID.toString())
				&& rst[elem.user.dbID]!=null){
					var track:TrackInfo = new TrackInfo();
					var song:Object = JSON.decode(rst[elem.user.dbID].song as String );
					DataUtil.parseChaosRecommendTrackInfo(song,track);
					elem.setRecentLikeTrackId(track.sid,track.name);
				}else{
					elem.setRecentLikeTrackId(-1,null);
				}
			}
		}
		
		private function onClearLblClick(e:MouseEvent):void{
			clearAllSelected();
		}
		
		private function onSelectedValueChange(e:Event):void{
			dispatchEvent(new Event(UserGroup.USER_GROUP_SELECTED_CHANGE));
		}
		
		private function onReceiverLiked(e:RecommendEvent):void{
			var info : ChaosEvtInfo = e.data as ChaosEvtInfo;
			if(info!=null){
				var userArr:Array = Format.getEvtUserInfo(info.content);
				if(userArr.length>0){
					var searchIdx:int = getGroupIdx(userArr[0].uid as int);
					if(searchIdx>=0){
						var tracks:Array = Format.getEvtTrackInfo(info.content);
						var reTrack:Object = tracks.length>0?tracks[0]:null;
						if(reTrack!=null){
							var elem:UserQuickSlectViewItem = _list[searchIdx] as UserQuickSlectViewItem;
							if(elem.user.dbID == userArr[0].uid as int){
								if(elem.getRecentLikeTrackId()==-1 || elem.getRecentLikeTrackId()!= (reTrack.sid as int)){
									//UPDATE THE RECENT LIKE TRACK
									elem.setRecentLikeTrackId(reTrack.sid as int,reTrack.name as String);
								}
							}
						}
					}
				}
			}
		}
		
		private function onFriendSiteOnAir(e:P2pEvent):void{
			var siteID:int = e.data as int;
			var searchIdx:int =  getGroupIdx(siteID);
			if(searchIdx>=0){
				var elem:UserQuickSlectViewItem = _list[searchIdx] as UserQuickSlectViewItem;
				if(elem.user.dbID == siteID){
					elem.setUserOnAir();
				}
			}
		}
		
		private function onFriendSiteNotOnAir(e:P2pEvent):void{
			var siteID:int = e.data as int;
			var searchIdx:int =  getGroupIdx(siteID);
			if(searchIdx>=0){
				var elem:UserQuickSlectViewItem = _list[searchIdx] as UserQuickSlectViewItem;
				if(elem.user.dbID == siteID){
					elem.setUserNotOnAir();
				}
			}
		}
		
		private function getGroupIdx(searchUID:int):int
		{
			if(_voList!=null){
				for(var i:int=0;i<_voList.length;i++){
					if((_voList[i] as UserInfo).dbID == searchUID)
						return i;
				}
				
				return -1;
			}else
				return -1;
		}
	}
}